﻿using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
using LosConn.BLL;
using System.Web.SessionState;

namespace LosConn.Web.Handler
{
    /// <summary>
    /// ProductHandler 的摘要说明
    /// </summary>
    public class ProductHandler : IHttpHandler, IReadOnlySessionState
    {
        private BLL.Los_Product bll = new BLL.Los_Product();
        private BLL.Los_Type bllType = new BLL.Los_Type();
        public void ProcessRequest(HttpContext context)
        {
            String strResult = String.Empty;
            switch (context.Request["fun"])
            {

                case "f_0001": //新增产品
                    strResult = AddProduct(context);
                    break;
                case "f_0002": //修改产品
                    strResult = UpdateProduct(context);
                    break;
                case "f_0003": //假删除产品
                    strResult = UpdateProductStatus(context);
                    break;
                case "f_0004": //新增产品类别
                    strResult = AddType(context);
                    break;
                case "f_0005": //修改产品类别
                    strResult = UpdateType(context);
                    break;
                case "f_0006": //假删除产品类别
                    strResult = UpdateTypeStatus(context);
                    break;
                default:
                    break;
            }

            context.Response.Write(strResult);
            context.Response.End();
        }

        #region 私有方法
        /// <summary>
        /// 新增产品
        /// </summary>
        private string AddProduct(HttpContext context)
        {
            Model.Los_Product product = JsonConvert.DeserializeObject<Model.Los_Product>(context.Request["data"]);
            product.Product_UserID = Convert.ToInt32(context.Session["UserID"]);
            product.Product_UserName = context.Session["UserName"].ToString();
            if (bll.Add(product) > 0)
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 修改产品
        /// </summary>
        private string UpdateProduct(HttpContext context)
        {
            Model.Los_Product product = JsonConvert.DeserializeObject<Model.Los_Product>(context.Request["data"]);
            product.Product_UserID = Convert.ToInt32(context.Session["UserID"]);
            product.Product_UserName = context.Session["UserName"].ToString();
            product.Product_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.Update(product))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 假删除产品
        /// </summary>
        private string UpdateProductStatus(HttpContext context)
        {
            Model.Los_Product product = new Model.Los_Product();
            product.Product_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bll.UpdateStatus(product))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 新增产品类别
        /// </summary>
        private string AddType(HttpContext context)
        {
            Model.Los_Type model = JsonConvert.DeserializeObject<Model.Los_Type>(context.Request["data"]);
            model.Type_UserID = Convert.ToInt32(context.Session["UserID"]);
            model.Type_UserName = context.Session["UserName"].ToString();
            if (bllType.Add(model) > 0)
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 修改产品类别
        /// </summary>
        private string UpdateType(HttpContext context)
        {
            Model.Los_Type model = JsonConvert.DeserializeObject<Model.Los_Type>(context.Request["data"]);
            model.Type_UserID = Convert.ToInt32(context.Session["UserID"]);
            model.Type_UserName = context.Session["UserName"].ToString();
            model.Type_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bllType.Update(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }

        /// <summary>
        /// 删除产品类别
        /// </summary>
        private string UpdateTypeStatus(HttpContext context)
        {
            Model.Los_Type model = new Model.Los_Type();
            model.Type_ID = Convert.ToInt32(context.Request.Form["id"]);
            if (bllType.UpdateStatus(model))
            {
                return "{" + WebClass.RESULT_CODE_SUCCESS + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_SUCCESS_LAN + "\"}";

            }
            else
            {
                return "{" + WebClass.RESULT_CODE_ERROR + "," + WebClass.RESULT_INFO + "\"" + WebClass.RESULT_ERROR_LAN + "\"}";
            }
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}